<?php

// TITLE: contact
// FILE: contact.php
// AUTHOR: dantira
// CREATED: Mar 21, 2015, 4:51:41 PM
?>

		public function createContact($id, $addressId, $phone, $email) {
			global $con;
			$sql = 'INSERT INTO contact (id, address_id, phone, email) VALUES (' . $id . ', ' . $addressId . ', ' . $phone . ', ' . $email . ')';
			$this->open();
			$result = mysqli_query($con, $sql);
			$id = ($result) ? mysqli_insert_id($con) : $result;
			$this->close();
			return $id;
		}// end function

		public function readContact($id) {
			global $con;
			$sql = 'SELECT * FROM contact WHERE id = ' . $id;
			$this->open();
			$results = mysqli_query($con, $sql);
			$this->close();
			if ($results) {
				$result = mysqli_fetch_array($results);
				$contact = new Contact();
				$contact->setId($result[0]);
				$contact->setAddressId($this->readAddress($result[1]));
				$contact->setPhone($result[2]);
				$contact->setEmail($result[3]);
			} else {
				$contact = false;
			}
			return $contact;
		}// end function

		public function updateContact($id, $addressId, $phone, $email) {
			global $con;
			$sql = 'UPDATE contact SET address_id = ' . $address_id . ', phone = ' . $phone . ', email = ' . $email . ' WHERE id = ' . $id;
			$this->open();
			$result = mysqli_query($con, $sql);
			$this->close();
			return $result;
		}// end function

		public function deleteContact($id) {
			global $con;
			$sql = 'DELETE FROM contact WHERE id = ' . $id;
			$this->open();
			$result = mysqli_query($con, $sql);
			$this->close();
			return $result;
		}// end function

		public function listContact() {
			global $con;
			$sql = 'SELECT * FROM contact';
			$this->open();
			$results = mysqli_query($con, $sql);
			$this->close();
			if ($results) {
				$contacts = array();
				while ($result = mysqli_fetch_array($results)) {
					$contact = new Contact();
					$contact->setId($result[0]);
					$contact->setAddressId($this->readAddress($result[1]));
					$contact->setPhone($result[2]);
					$contact->setEmail($result[3]);
					$contacts[] = $contact;
				}// end while
			} else {
				$contacts = false;
			}
			return $contacts;
		}// end function
